 clear all
set more off

*cd "/Users/michaelhotard/Dropbox (IPL)/fee waiver/"
cd "/Users/yasenov/Dropbox (IPL)/fee waiver/"

use "data/usa_00132.dta"
sum age
keep if year >= 2007 & year <= 2016

****************
* demographics
****************
gen byte veteran = vetstat == 2
drop vetstat

gen byte male = sex == 1
drop sex

gen byte married = inlist(marst,1,2)
drop marst

gen byte unempl = empstat == 2
gen byte notinlf = empstat == 3
drop empstat

gen byte medicaid = hinscaid == 2
drop hinscaid

gen byte english = language == 1
drop language

replace incwage = incwage / 1e4

sum inc*
gen byte means_tested = incwelfr > 0 | medicaid == 1 | incsupp > 0  // all these variables are at the individual level. ACS has a food stamp dummy but it is measured at the household level

*gen byte poor = poverty <= 150

* MARRIED TO NATIVE
gen nat = citizen <= 2
gen nat_nokids = nat  // variable indicating whether either spouse is native
replace nat_nokids = 0 if relate > 2 // don't count kids & other non-spouse members as "natives"
egen nat_hshld_head = total(nat_nokids), by(year serial datanum)
replace nat_hshld_head = min(1, nat_hshld_head)
gen byte married_to_citizen = married == 1 & relate <= 2 & nat_hshld_head == 1
drop nat_nokids

replace married = 0 if married_to_citizen == 1   // married means married to a non-citizen

do "code/misc_bpl_recode.do"

gen byte white = race == 1
gen byte black = race == 2
gen byte asian = inlist(race, 4,5,6)

gen agesq = age* age
gen yrsusasq = yrsusa1 * yrsusa1

gen inmetro = metro == 2
recode educ (1 = 3) (2=7) (3=9) (4=10) (5=11) (6=12) (7=13) (8=14) (10=16) (11=18)

****************
* sample selection
****************
keep if age >= 18 & age <= 65

* born in the US
drop if bpl <= 120
tab citizen, sort

drop if citizen == 0 | citizen == 1

* undocumented
drop if bpl == 200 & inlist(citizen, 3,4) & educ <= 12   // Foreign-born Mexicans with high school diploma or less

* students
drop if (age >= 18 & age <= 24) & school == 2
drop school

* non low-income
merge m:1 year famsize using "data/povertyGuidelines.dta", keep(1 3) 
assert _merge == 3
drop _merge

replace ftotinc = . if ftotinc <= 0 | ftotinc == 9999999
keep if ftotinc <= 3 * fpl
* drop if poverty > 300

gen poor = 0
replace poor = 1 if ftotinc <= 1.5 * fpl & statefip != 2 & statefip != 15 // not alaska, hawaii
replace poor = 1 if ftotinc <= 1.5 * fpl_alaska & statefip == 2  //  alaska
replace poor = 1 if ftotinc <= 1.5 * fpl_hawaii & statefip == 15  //  hawaii

replace poverty = ftotinc/fpl if statefip != 2 & statefip != 15   // not alaska, hawaii
replace poverty = ftotinc/fpl_alaska if statefip == 2	// alaska
replace poverty = ftotinc/fpl_hawaii if statefip == 15	// hawaii

* recent immigrants
*keep if yrsusa1 >= 6 | (yrsusa1 >= 4 & married_to_citizen == 1)
keep if yrsusa1 >= 6 | (yrsusa1 >= 4 & married == 1 & nat_hshld_head == 1 & relate <= 2)
*keep if yrsusa1 >= 5 | (yrsusa1 >= 3 & married == 1 & nat_hshld_head == 1 & relate <= 2)
sum yrsusa1

merge m:1 statefip year using "data/local_controls.dta", nogen keep(3)

keep if yrsusa1 <= 40

drop if veteran == 1

************
* treatment
************

gen byte treatment = (poor == 1) | (means_tested == 1)
tab treatment, m

gen byte interaction = treatment * (year >= 2012)
label var interaction "Difference-in-Differences"

xtile poor_tercile = poverty if treatment == 1, nq(3)

***********
* outcome
***********
gen byte y = citizen == 2
tab y

sum
tab year treatment
qui compress

bysort treatment: sum y yrsusa1 poverty age

****************
* summary stats
****************
local sumvars male age white black asian inmetro educ married married_to_citizen yrsusa1 y english incwage unempl notinlf poverty medicaid incwelfr

qui do "code/var_lables.do"

eststo clear
eststo: qui estpost sum `sumvars' [aw=perwt]
bysort treatment: eststo: qui estpost sum `sumvars' [aw=perwt]

esttab,	///
	cells("mean(fmt(2)) sd(fmt(2))") ///
	nonumber label ///
	mtitles("Overall" "Control" "Treatment")

esttab using "si/sum_stats.tex",	///
	cells("mean(fmt(2)) sd(fmt(2))") ///
	nonumber replace ///
	mtitles("Overall" "Control" "Treatment") ///
	label

****************
* regressions
****************	
global controls white black asian male married incwage ///
	unempl notinlf inmetro educ age agesq yrsusasq yrsusa1 ///
	i.year married_to_citizen english unemp lweekly // statefip#c.year
global options absorb(statefip bplgroup) vce(cluster statefip) 

****************
* logit
****************
logit y $controls i.bplgroup if year < 2012 [pw=perwt]
predict yhat, pr    

xtile yhat_tercile = yhat, nq(3)    // same as tercile of odds
tab yhat_tercile

* MAIN
eststo clear
eststo: reghdfe y treatment interaction $controls [pw=perwt], $options
eststo: reghdfe y treatment interaction $controls if year >= 2008 & year <= 2015 [pw=perwt], $options
eststo: reghdfe y treatment interaction $controls if year >= 2009 & year <= 2014 [pw=perwt], $options
eststo: reghdfe y treatment interaction $controls if year >= 2010 & year <= 2013 [pw=perwt], $options

esttab, keep(interaction) ///
	b(%4.3f) se(%4.3f) ///
	mtitles("2007-2016" "2008-2015" "2009-2014" "2010-2013")

esttab using "si/main.tex", keep(interaction) ///
	b(%4.3f) se(%4.3f) ///
	nonum nonotes label ar2 replace star(* .1 ** .05 *** .01) ///
	mtitles("2007-2016" "2008-2015" "2009-2014" "2010-2013")

* fig 2	
reghdfe y treatment treatment#i.year $controls [pw=perwt], $options

preserve
parmest, fast
keep if substr(parm,1,5) == "1.tre"
gen year = substr(parm,-9,4)
destring year, replace
keep est min max year
set obs 10
replace year = 2007 in 10
replace est = 0 if year == 2007
	
twoway (scatter est year, mcolor(blue)) ///
	(rcap min max year, lcolor(black)), ///
	legend(off) ///
	yline(0) ///
	xline(2011.5) ///
	xtitle("Year") /// 
	ytitle("Difference in Naturalization Rate b/w Treatment and Control" "(percentage points)") ///
	xlabel(2007(1)2017) ///
	ylabel(-.02 "-2" -.01 "-1" 0 "0" .01 "1" .02 "2" .03 "3" .04 "4" .05 "5") 
*graph save "si/fig2.gph", replace		
graph export "si/fig2.png", replace	
restore	

* METRO
preserve
eststo clear
keep if metro == 2
eststo: qui reghdfe y treatment interaction $controls [pw=perwt], $options
eststo: qui reghdfe y treatment interaction $controls if year >= 2008 & year <= 2015 [pw=perwt], $options
eststo: qui reghdfe y treatment interaction $controls if year >= 2009 & year <= 2014 [pw=perwt], $options
eststo: qui reghdfe y treatment interaction $controls if year >= 2010 & year <= 2013 [pw=perwt], $options

esttab, keep(interaction) ///
	b(%4.3f) se(%4.3f) ///
	mtitles("2007-2016" "2008-2015" "2009-2014" "2010-2013")
	
esttab using "si/metro.tex", keep(interaction) ///
	b(%4.3f) se(%4.3f) ///
	nonum nonotes label ar2 replace star(* .1 ** .05 *** .01) ///
	mtitles("2007-2016" "2008-2015" "2009-2014" "2010-2013") ///
	prehead("{\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}\begin{tabular}{l*{4}{c}} \\  &\multicolumn{4}{c}{Panel A: In Central City}       \\ \cmidrule(lr){2-5}  ") ///
	postfoot("")	
restore

* NOT METRO
preserve
eststo clear
keep if metro != 2
eststo: qui reghdfe y treatment interaction $controls [pw=perwt], $options
eststo: qui reghdfe y treatment interaction $controls if year >= 2008 & year <= 2015 [pw=perwt], $options
eststo: qui reghdfe y treatment interaction $controls if year >= 2009 & year <= 2014 [pw=perwt], $options
eststo: qui reghdfe y treatment interaction $controls if year >= 2010 & year <= 2013 [pw=perwt], $options

esttab, keep(interaction) ///
	b(%4.3f) se(%4.3f) ///
	mtitles("2007-2016" "2008-2015" "2009-2014" "2010-2013")
	
esttab using "si/metro.tex", keep(interaction) ///
	b(%4.3f) se(%4.3f) ///
	nonum nonotes label ar2 append star(* .1 ** .05 *** .01) ///
	mtitles("2007-2016" "2008-2015" "2009-2014" "2010-2013") ///
	prehead("\\ &\multicolumn{4}{c}{Panel B: Not In Central City}       \\ \cmidrule(lr){2-5}  ")
restore


* NOT LING ISOLATED
preserve
eststo clear
keep if ling != 2
eststo: qui reghdfe y treatment interaction $controls [pw=perwt], $options
eststo: qui reghdfe y treatment interaction $controls if year >= 2008 & year <= 2015 [pw=perwt], $options
eststo: qui reghdfe y treatment interaction $controls if year >= 2009 & year <= 2014 [pw=perwt], $options
eststo: qui reghdfe y treatment interaction $controls if year >= 2010 & year <= 2013 [pw=perwt], $options

esttab, keep(interaction) ///
	b(%4.3f) se(%4.3f) ///
	mtitles("2007-2016" "2008-2015" "2009-2014" "2010-2013")
	
esttab using "si/language.tex", keep(interaction) ///
	b(%4.3f) se(%4.3f) ///
	nonum nonotes label ar2 replace star(* .1 ** .05 *** .01) ///
	mtitles("2007-2016" "2008-2015" "2009-2014" "2010-2013") ///
	prehead("{\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}\begin{tabular}{l*{4}{c}} \\  &\multicolumn{4}{c}{Panel A: Households with an English Speaker}       \\ \cmidrule(lr){2-5}  ") ///
	postfoot("")	
restore

* LING ISOLATED
preserve
eststo clear
keep if ling == 2
eststo: qui reghdfe y treatment interaction $controls [pw=perwt], $options
eststo: qui reghdfe y treatment interaction $controls if year >= 2008 & year <= 2015 [pw=perwt], $options
eststo: qui reghdfe y treatment interaction $controls if year >= 2009 & year <= 2014 [pw=perwt], $options
eststo: qui reghdfe y treatment interaction $controls if year >= 2010 & year <= 2013 [pw=perwt], $options

esttab, keep(interaction) ///
	b(%4.3f) se(%4.3f) ///
	mtitles("2007-2016" "2008-2015" "2009-2014" "2010-2013")
	
esttab using "si/language.tex", keep(interaction) ///
	b(%4.3f) se(%4.3f) ///
	nonum nonotes label ar2 append star(* .1 ** .05 *** .01) ///
	mtitles("2007-2016" "2008-2015" "2009-2014" "2010-2013") ///
	prehead("\\ &\multicolumn{4}{c}{Panel B: Households without an English Speaker}       \\ \cmidrule(lr){2-5}  ")	
restore

* EDUC <= 12
preserve
eststo clear
keep if educ <= 12
eststo: qui reghdfe y treatment interaction $controls [pw=perwt], $options
eststo: qui reghdfe y treatment interaction $controls if year >= 2008 & year <= 2015 [pw=perwt], $options
eststo: qui reghdfe y treatment interaction $controls if year >= 2009 & year <= 2014 [pw=perwt], $options
eststo: qui reghdfe y treatment interaction $controls if year >= 2010 & year <= 2013 [pw=perwt], $options

esttab, keep(interaction) ///
	b(%4.3f) se(%4.3f) ///
	mtitles("2007-2016" "2008-2015" "2009-2014" "2010-2013")

esttab using "si/educ.tex", keep(interaction) ///
	b(%4.3f) se(%4.3f) ///
	nonum nonotes label ar2 replace star(* .1 ** .05 *** .01) ///
	mtitles("2007-2016" "2008-2015" "2009-2014" "2010-2013") ///
	prehead("{\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}\begin{tabular}{l*{4}{c}} \\  &\multicolumn{4}{c}{Panel A: High School or Lower}       \\ \cmidrule(lr){2-5}  ") ///
	postfoot("")		
restore


* EDUC > 12
preserve
eststo clear
keep if educ > 12
eststo: qui reghdfe y treatment interaction $controls [pw=perwt], $options
eststo: qui reghdfe y treatment interaction $controls if year >= 2008 & year <= 2015 [pw=perwt], $options
eststo: qui reghdfe y treatment interaction $controls if year >= 2009 & year <= 2014 [pw=perwt], $options
eststo: qui reghdfe y treatment interaction $controls if year >= 2010 & year <= 2013 [pw=perwt], $options

esttab, keep(interaction) ///
	b(%4.3f) se(%4.3f) ///
	mtitles("2007-2016" "2008-2015" "2009-2014" "2010-2013")
	
esttab using "si/educ.tex", keep(interaction) ///
	b(%4.3f) se(%4.3f) ///
	nonum nonotes label ar2 append star(* .1 ** .05 *** .01) ///
	mtitles("2007-2016" "2008-2015" "2009-2014" "2010-2013") ///
	prehead("\\ &\multicolumn{4}{c}{Panel B:  Some College or More}       \\ \cmidrule(lr){2-5}  ")		
restore


* YHAT = 1
preserve
eststo clear
keep if yhat_tercile == 1
eststo: qui reghdfe y treatment interaction $controls [pw=perwt], $options
eststo: qui reghdfe y treatment interaction $controls if year >= 2008 & year <= 2015 [pw=perwt], $options
eststo: qui reghdfe y treatment interaction $controls if year >= 2009 & year <= 2014 [pw=perwt], $options
eststo: qui reghdfe y treatment interaction $controls if year >= 2010 & year <= 2013 [pw=perwt], $options

esttab, keep(interaction) ///
	b(%4.3f) se(%4.3f) ///
	mtitles("2007-2016" "2008-2015" "2009-2014" "2010-2013")
	
esttab using "si/yhat.tex", keep(interaction) ///
	b(%4.3f) se(%4.3f) ///
	nonum nonotes label ar2 replace star(* .1 ** .05 *** .01) ///
	mtitles("2007-2016" "2008-2015" "2009-2014" "2010-2013") ///
	prehead("{\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}\begin{tabular}{l*{4}{c}} \\  &\multicolumn{4}{c}{Panel A: Bottom Tercile}       \\ \cmidrule(lr){2-5}  ") ///
	postfoot("")		
restore


* YHAT = 2
preserve
eststo clear
keep if yhat_tercile == 2
eststo: qui reghdfe y treatment interaction $controls [pw=perwt], $options
eststo: qui reghdfe y treatment interaction $controls if year >= 2008 & year <= 2015 [pw=perwt], $options
eststo: qui reghdfe y treatment interaction $controls if year >= 2009 & year <= 2014 [pw=perwt], $options
eststo: qui reghdfe y treatment interaction $controls if year >= 2010 & year <= 2013 [pw=perwt], $options

esttab, keep(interaction) ///
	b(%4.3f) se(%4.3f) ///
	mtitles("2007-2016" "2008-2015" "2009-2014" "2010-2013")
	
esttab using "si/yhat.tex", keep(interaction) ///
	b(%4.3f) se(%4.3f) ///
	nonum nonotes label ar2 append star(* .1 ** .05 *** .01) ///
	mtitles("2007-2016" "2008-2015" "2009-2014" "2010-2013") ///
	prehead("\\ &\multicolumn{4}{c}{Panel B:  Middle Tercile}       \\ \cmidrule(lr){2-5}  ")	///
	postfoot("")
restore


* YHAT = 3
preserve
eststo clear
keep if yhat_tercile == 3
eststo: qui reghdfe y treatment interaction $controls [pw=perwt], $options
eststo: qui reghdfe y treatment interaction $controls if year >= 2008 & year <= 2015 [pw=perwt], $options
eststo: qui reghdfe y treatment interaction $controls if year >= 2009 & year <= 2014 [pw=perwt], $options
eststo: qui reghdfe y treatment interaction $controls if year >= 2010 & year <= 2013 [pw=perwt], $options

esttab, keep(interaction) ///
	b(%4.3f) se(%4.3f) ///
	mtitles("2007-2016" "2008-2015" "2009-2014" "2010-2013")

esttab using "si/yhat.tex", keep(interaction) ///
	b(%4.3f) se(%4.3f) ///
	nonum nonotes label ar2 append star(* .1 ** .05 *** .01) ///
	mtitles("2007-2016" "2008-2015" "2009-2014" "2010-2013") ///
	prehead("\\ &\multicolumn{4}{c}{Panel C:  Top Tercile}       \\ \cmidrule(lr){2-5}  ")		
restore

* POVERTY 1
preserve
keep if poor_tercile == 1 | treatment == 0
eststo clear
eststo: reghdfe y treatment interaction $controls [pw=perwt], $options
eststo: reghdfe y treatment interaction $controls if year >= 2008 & year <= 2015 [pw=perwt], $options
eststo: reghdfe y treatment interaction $controls if year >= 2009 & year <= 2014 [pw=perwt], $options
eststo: reghdfe y treatment interaction $controls if year >= 2010 & year <= 2013 [pw=perwt], $options


esttab, keep(interaction) ///
	b(%4.3f) se(%4.3f) ///
	mtitles("2007-2016" "2008-2015" "2009-2014" "2010-2013")

esttab using "si/poverty.tex", keep(interaction) ///
	b(%4.3f) se(%4.3f) ///
	nonum nonotes label ar2 replace star(* .1 ** .05 *** .01) ///
	mtitles("2007-2016" "2008-2015" "2009-2014" "2010-2013") ///
	prehead("{\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}\begin{tabular}{l*{4}{c}} \\  &\multicolumn{4}{c}{Panel A: Bottom Tercile}       \\ \cmidrule(lr){2-5}  ") ///
	postfoot("")	
restore

* POVERTY 2
preserve
keep if poor_tercile == 2 | treatment == 0
eststo clear
eststo: reghdfe y treatment interaction $controls [pw=perwt], $options
eststo: reghdfe y treatment interaction $controls if year >= 2008 & year <= 2015 [pw=perwt], $options
eststo: qui reghdfe y treatment interaction $controls if year >= 2009 & year <= 2014 [pw=perwt], $options
eststo: reghdfe y treatment interaction $controls if year >= 2010 & year <= 2013 [pw=perwt], $options


esttab, keep(interaction) ///
	b(%4.3f) se(%4.3f) ///
	mtitles("2007-2016" "2008-2015" "2009-2014" "2010-2013")

esttab using "si/poverty.tex", keep(interaction) ///
	b(%4.3f) se(%4.3f) ///
	nonum nonotes label ar2 append star(* .1 ** .05 *** .01) ///
	mtitles("2007-2016" "2008-2015" "2009-2014" "2010-2013") ///
	prehead("\\ &\multicolumn{4}{c}{Panel B:  Middle Tercile}       \\ \cmidrule(lr){2-5}  ")	///
	postfoot("")	
restore

* POVERTY 3
preserve
keep if poor_tercile == 3 | treatment == 0
eststo clear
eststo: reghdfe y treatment interaction $controls [pw=perwt], $options
eststo: reghdfe y treatment interaction $controls if year >= 2008 & year <= 2015 [pw=perwt], $options
eststo: qui reghdfe y treatment interaction $controls if year >= 2009 & year <= 2014 [pw=perwt], $options
eststo: reghdfe y treatment interaction $controls if year >= 2010 & year <= 2013 [pw=perwt], $options


esttab, keep(interaction) ///
	b(%4.3f) se(%4.3f) ///
	mtitles("2007-2016" "2008-2015" "2009-2014" "2010-2013")

esttab using "si/poverty.tex", keep(interaction) ///
	b(%4.3f) se(%4.3f) ///
	nonum nonotes label ar2 append star(* .1 ** .05 *** .01) ///
	mtitles("2007-2016" "2008-2015" "2009-2014" "2010-2013") ///
	prehead("\\ &\multicolumn{4}{c}{Panel C:  Top Tercile}       \\ \cmidrule(lr){2-5}  ")		
restore

****************
**************** P-VALUES
****************
* P-VALUE LANGUAGE
reghdfe y treatment interaction $controls if (year >= 2009 & year <= 2014) & (ling != 2) [pw=perwt], $options
scalar A1 = _b[interaction]
scalar A2 = _se[interaction]

qui reghdfe y treatment interaction $controls if (year >= 2009 & year <= 2014 )  & (ling == 2) [pw=perwt], $options
scalar B1 = _b[interaction]
scalar B2 = _se[interaction]

scalar Z1 = (A1-B1)/(sqrt(B2^2+A2^2))

* P-VALUE EDUCATION
qui reghdfe y treatment interaction $controls if (year >= 2009 & year <= 2014) & (educ <= 12) [pw=perwt], $options
scalar C1 = _b[interaction]
scalar C2 = _se[interaction]

qui reghdfe y treatment interaction $controls if (year >= 2009 & year <= 2014) & (educ > 12) [pw=perwt], $options
scalar D1 = _b[interaction]
scalar D2 = _se[interaction]

scalar Z2 = (D1-C1)/(sqrt(C2^2+D2^2))

* P-VALUE PROPENSITY TO NATURALIZE
qui reghdfe y treatment interaction $controls if (year >= 2009 & year <= 2014) & (yhat_tercile == 1) [pw=perwt], $options
scalar E1 = _b[interaction]
scalar E2 = _se[interaction]

qui reghdfe y treatment interaction $controls if (year >= 2009 & year <= 2014) & (yhat_tercile == 3) [pw=perwt], $options
scalar F1 = _b[interaction]
scalar F2 = _se[interaction]

scalar Z3 = (F1-E1)/(sqrt(E2^2+F2^2))

* P-VALUE INCOME
qui reghdfe y treatment interaction $controls if (year >= 2009 & year <= 2014) & (poor_tercile == 1 | treatment == 0) [pw=perwt], $options
scalar G1 = _b[interaction]
scalar G2 = _se[interaction]

qui reghdfe y treatment interaction $controls if (year >= 2009 & year <= 2014) & (poor_tercile == 3 | treatment == 0) [pw=perwt], $options
scalar H1 = _b[interaction]
scalar H2 = _se[interaction]

scalar Z4 = (H1-G1)/(sqrt(H2^2+G2^2))

di 2 * normal(Z1) // language
di 2 * normal(Z2) // education
di 2 * normal(Z3) // propensity to nat
di 2 * normal(Z4) // income


****************
**************** * IMMIGRANT SERVICE PROVIDERS
****************
merge m:1 statefip using "data/isp.dta"
assert _merge == 3
drop _merge

bysort statefip: egen pop = total(perwt)
replace pop = pop / 10    // because there are 10 years in the data

sum isp* ngo* pop

replace isp_urban = 0 if isp_urban == .    // !!!

*gen isp_urban_pc = (isp_urban / pop) * 10000     // per 10, 000 people
gen isp_lsd_pc = (isp_lsd / pop) * 10000  
gen isp_catholic_pc = (isp_catholic / pop) * 10000
gen ngo_taxreturn_pc = (ngo_taxreturn / pop) * 10000

*tab isp_urban  
*tab isp_lsd       // median is 14; 33rd percentile is 6.5; 66th percentile is 18
*tab isp_catholic  // median is 6; 33rd percentile is 3; 66th percentile is 7
*tab ngo_taxreturn  // median is 4448; 33rd percentile is 2623; 66th percentile is 7679.5

*tab isp_urban_pc
*tab isp_lsd_pc    // median is 2.04; 33rd percentile is 1.43; 66th percentile is 2.66
*tab isp_catholic_pc  // median is 0.8; 33rd percentile is 0.47; 66th percentile is 1.10
*tab ngo_taxreturn_pc  // median is 894.1; 33rd percentile is 574.82; 66th percentile is 1302.83


egen cluster = group(statefip year)
global options absorb(statefip bplgroup) vce(cluster cluster)  

eststo clear
preserve
keep if isp_lsd_pc < 2.04  &  inmetro == 1

eststo: qui reghdfe y treatment interaction $controls [pw=perwt], $options
eststo: qui reghdfe y treatment interaction $controls if year >= 2008 & year <= 2015 [pw=perwt], $options
eststo: qui reghdfe y treatment interaction $controls if year >= 2009 & year <= 2014 [pw=perwt], $options
eststo: qui reghdfe y treatment interaction $controls if year >= 2010 & year <= 2013 [pw=perwt], $options

esttab, keep(interaction) ///
	b(%4.3f) se(%4.3f) ///
	mtitles("2007-2016" "2008-2015" "2009-2014" "2010-2013")

esttab using "si/nonprofits.tex", keep(interaction) ///
	b(%4.3f) se(%4.3f) ///
	nonum nonotes label ar2 replace star(* .1 ** .05 *** .01) ///
	mtitles("2007-2016" "2008-2015" "2009-2014" "2010-2013") ///
	prehead("{\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}\begin{tabular}{l*{4}{c}} \\  &\multicolumn{4}{c}{Panel A: Below Median ISPs per LPR}       \\ \cmidrule(lr){2-5}  ") ///
	postfoot("")	
restore

eststo clear	
preserve
keep if isp_lsd_pc >= 2.04 & inmetro == 1

eststo: qui reghdfe y treatment interaction $controls [pw=perwt], $options
eststo: qui reghdfe y treatment interaction $controls if year >= 2008 & year <= 2015 [pw=perwt], $options
eststo: qui reghdfe y treatment interaction $controls if year >= 2009 & year <= 2014 [pw=perwt], $options
eststo: qui reghdfe y treatment interaction $controls if year >= 2010 & year <= 2013 [pw=perwt], $options

esttab, keep(interaction) ///
	b(%4.3f) se(%4.3f) ///
	mtitles("2007-2016" "2008-2015" "2009-2014" "2010-2013")

esttab using "si/nonprofits.tex", keep(interaction) ///
	b(%4.3f) se(%4.3f) ///
	nonum nonotes label ar2 append star(* .1 ** .05 *** .01) ///
	mtitles("2007-2016" "2008-2015" "2009-2014" "2010-2013") ///
	prehead("\\ &\multicolumn{4}{c}{Panel B: Above Median ISPs per LPR}       \\ \cmidrule(lr){2-5}  ")	
restore


* P-VALUE IMMIGRANT SERVICE PROVIDERS
qui reghdfe y treatment interaction $controls if (year >= 2009 & year <= 2014) & (isp_lsd_pc < 2.04 & inmetro == 1) [pw=perwt], $options
scalar I1 = _b[interaction]
scalar I2 = _se[interaction]

qui reghdfe y treatment interaction $controls if (year >= 2009 & year <= 2014 )  & (isp_lsd_pc >= 2.04 & inmetro == 1) [pw=perwt], $options
scalar J1 = _b[interaction]
scalar J2 = _se[interaction]

scalar Z5 = (I1-J1)/(sqrt(J2^2+I2^2))

di 2 * normal(Z5)


****************
* marginal effects 
****************
* marginal effects 
eststo clear
reg y $controls i.bplgroup [pw=perwt], vce(cluster state)    // *margins, dydx($controls) atmeans
qui do "code/var_lables2.do"

esttab using "si/margins.tex", ///
	keep(male age agesq white black asian inmetro educ married married_to_citizen english yrsusa1 yrsusasq incwage unempl notinlf) ///
	order(male age agesq white black asian inmetro educ married married_to_citizen english yrsusa1 yrsusasq incwage unempl notinlf) ///
	label nonum nonotes replace ///
	cells("b(fmt(3) star) se(fmt(3) par)") starlevels(* 0.1 ** 0.05 *** 0.01) ar2


*************
* raw graph
*************
preserve
collapse (mean) y [pw=perwt], by(year treatment)
sum
reshape wide y, i(year) j(treatment)
rename (y1 y0 ) (y_t y_c)
sum
list in 1/10

twoway (line y_t year, lpattern(solid)) ///
	(line y_c year, lpattern(dash)), ///
	legend(label(1 "Treatment") label(2 "Control")) ///
	xline(2011) ///
	ylabel(.48(.02).62) ///
	xtitle("Year") ///
	ytitle("Naturalization Rate")
graph save "si/figa1.gph", replace
graph export "si/figa1.png", replace	
restore
